package com.smsservice.activities;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.smsservice.R;

/**
 * Classe para salvar nas preferencias as configura������es do servidor de banco de
 * dados
 * 
 * @author Vitor Morelli - vitao.miacri@gmail.com
 * @since 30/06/2013
 */
public class ConfiguracaoServidor extends Activity {

	// Constante com o nome das preferencias salvas
	public static final String PREFS_NAME = "ConfigServidorPref";

	// Bot���es da tela
	EditText edEnderecoIP;
	EditText edPorta;
	EditText edNomeBaseDados;
	EditText edNomeUsuario;
	EditText edSenha;
	Button btnSalvar;

	String dbName;
	String userName;
	String password;

	String enderecoIP;
	String porta;
	String nomeBaseDados;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);

		setContentView(R.layout.configuracoes);

		// Recupera os elementos da tela
		getViewsFromID();

		// Restaura as preferencias gravadas
		SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
		if (settings != null) {
			enderecoIP = settings.getString("EnderecoIP", "");
			porta = String.valueOf(settings.getString("Porta", ""));
			dbName = settings.getString("NomeBaseDados", "");
			userName = settings.getString("nomeUsuario", "");
			password = settings.getString("Senha", "");

			edEnderecoIP.setText(enderecoIP);
			edPorta.setText(porta);
			edNomeBaseDados.setText(dbName);
			edNomeUsuario.setText(userName);
			edSenha.setText(password);
		}

	}

	public void salvaServidorPrefs(View v) {
		SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
		SharedPreferences.Editor editor = settings.edit();
		editor.putString("EnderecoIP", edEnderecoIP.getText().toString());
		editor.putString("Porta", edPorta.getText().toString());
		editor.putString("NomeBaseDados", edNomeBaseDados.getText().toString());
		editor.putString("nomeUsuario", edNomeUsuario.getText().toString());
		editor.putString("Senha", edSenha.getText().toString());

		editor.commit();

		Toast.makeText(this, "Configurações gravadas!", Toast.LENGTH_LONG).show();
		finish();

	}

	private void getViewsFromID() {
		edEnderecoIP = (EditText) findViewById(R.id.ed_ip_servidor);
		edPorta = (EditText) findViewById(R.id.ed_porta_servidor);
		edNomeBaseDados = (EditText) findViewById(R.id.ed_nome_db);
		edNomeUsuario = (EditText) findViewById(R.id.ed_usuario_db);
		edSenha = (EditText) findViewById(R.id.ed_senha_db);
		btnSalvar = (Button) findViewById(R.id.btn_salvar_config);
	}

}
